interface PopupObject extends eui.Component {
	popupBy: string;
	getGroup(): string;
	escNotClose?: boolean;
	onEsc(): void;
	// clearWhenLostFocus: boolean;
}

class PopupLayer extends UILayer {
	clearClass(Kclass: any): boolean {
		for (const c of this.$children) {
			if (c instanceof Kclass) {
				this.removeChild(c);
				MainWindow.ins().focus.popFocus(c);
				return true;
			}
		}
		return false;
	}

	popupClass(Kclass: any): any {
		for (const c of this.$children) {
			if (c instanceof Kclass) {
				this.setChildIndex(c, -1);
				return c;
			}
		}
		const dlg = new Kclass();
		this.popup(dlg);
		return dlg;
	}

	showBmp(bmp: egret.Bitmap): void {
		const group = new PopupPanel();
		allCenter(group);
		const rc = new eui.Rect(bmp.width, bmp.height, 0);
		rc.alpha = 0.5;
		group.addChild(rc);
		group.addChild(bmp);
		this.popup(group);
	}

	popup(win: PopupObject, effect = '', timems = 100): void {
		this.addChild(win);
		MainWindow.ins().focus.pushFocus(win);
		if (effect) {
			setEffectUI(win, effect, timems);
		}
	}

	async clearPopupEffect(
		win: PopupObject,
		group: string,
		effect: string,
		timems = 100,
	): Promise<void> {
		await asyncSetEffectUI(win, effect, timems);
		this.clearPopup(win, group);
	}

	clearPopup(win: PopupObject, group?: string): void {
		if (win.parent !== this) {
			console.error('clearPopup not parent', win);
			return;
		}
		this.removeChild(win);
		MainWindow.ins().focus.popFocus(win);
		if (group) {
			this.clearGroup(group);
		}
	}

	clearGroup(group: string): void {
		const l = [];
		for (const c of this.$children) {
			// eslint-disable-next-line dot-notation
			if (c['popupBy'] === group) {
				l.push(c);
			}
		}
		for (const c of l) {
			this.clearPopup(c);
		}
	}

	clearSameClass(win: PopupObject): number {
		const l = [];
		const cls = win.constructor;
		for (const c of this.$children) {
			if (c instanceof cls) {
				l.push(c);
			}
		}
		for (const c of l) {
			this.clearPopup(c);
		}
		if (l.length > 0) {
			console.log('clearSameClass', l.length);
		}
		return l.length;
	}

	static evtClearPopup = 'EVT_POPUP_CLEAR';

	onEscape(): boolean {
		if (this.$children.length > 0) {
			const child = this.$children[this.$children.length - 1] as PopupObject;
			child.onEsc();
			if (!child.escNotClose) {
				if (child.parent === this) {
					this.clearPopup(child, child.getGroup());
				}
			}
			// child.dispatchEventWith(PopupLayer.evtClearPopup,false,)
			return true;
		}
		return false;
	}

	closeChild(win: PopupObject): void {
		this.clearPopup(win, win.getGroup());
	}
}

class PopupPanelLayer extends PopupLayer {
	override popup(win: PopupPanel, effect = '', timems = 100): void {
		super.popup(win, effect, timems);
	}
}

class PopupWinLayer extends PopupLayer {
	override popup(win: PopupWindow, effect = '', timems = 100): void {
		super.popup(win, effect, timems);
	}

	override clearGroup(group: string): void {
		super.clearGroup(group);
		MainWindow.ins().popup.clearGroup(group);
	}
}
